package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.ArrayList;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import helper.TwoJiaMi;
import helper.FourJiaMi;
import pojo.DBHelper;

public class LoginController extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LoginController() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	@Override
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 *
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 *
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=gb2312");
		request.setCharacterEncoding("gb2312");

		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		// System.out.println(name);
		// System.out.println(pwd);

		//String sm4key = request.getParameter("sm4key");
		//String sm4iv = request.getParameter("sm4iv");
		//String decryptedKey = Sm2Decrypt.sm2Decrypt(sm4key);
		//String decryptedIv = Sm2Decrypt.sm2Decrypt(sm4iv);

		try {
			//String decryptedName = new String(Sm4Decrypt.decryptCbcPkcs5Padding(Sm4Decrypt.hexStrToByte(name), Sm4Decrypt.hexStrToByte(decryptedKey), Sm4Decrypt.hexStrToByte(decryptedIv)));
			// System.out.println(decryptedName);
			//String decryptedPwd = new String(Sm4Decrypt.decryptCbcPkcs5Padding(Sm4Decrypt.hexStrToByte(pwd), Sm4Decrypt.hexStrToByte(decryptedKey), Sm4Decrypt.hexStrToByte(decryptedIv)));
			// System.out.println(decryptedPwd);

			String sql = "select * from admin where name='"+name+"' and enpwd='"+TwoJiaMi.encrypt(pwd)+"' and enabled=1";
			String args[] = {"id","name","pwd","type","status"};
			System.out.print("--------------"+sql);
			DBHelper sb = new DBHelper();
			ArrayList al = sb.selectRow(args, sql);
			String responseText = "";

			if(al == null || al.size() == 0){
				responseText = "1";
//			request.setAttribute("message", responseText);
			}else{
				HttpSession session = request.getSession();
				session.setAttribute("adminlogin", al);
				responseText = "2";
				ArrayList adminlogin = (ArrayList)session.getAttribute("adminlogin");
//			request.setAttribute("message", responseText);
			}
//		response.sendRedirect("/twtweb/users/index.jsp?message="+responseText);
//		request.getRequestDispatcher("/users/index.jsp").forward(request, response);
			PrintWriter out = response.getWriter();
			out.print(responseText);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*String sql = "select * from admin where name='"+name+"' and pwd='"+pwd+"'";
		String args[] = {"id","name","pwd","type","status"};
		SelectBean sb = new SelectBean();
		ArrayList al = sb.selectRow(args, sql);
        String responseText = "";

		if(al == null || al.size() == 0){
			responseText = "1";
//			request.setAttribute("message", responseText);
		}else{
			HttpSession session = request.getSession();
			session.setAttribute("adminlogin", al);
			responseText = "2";
			ArrayList adminlogin = (ArrayList)session.getAttribute("adminlogin");
//			request.setAttribute("message", responseText);
		}
//		response.sendRedirect("/twtweb/users/index.jsp?message="+responseText);
//		request.getRequestDispatcher("/users/index.jsp").forward(request, response);
		PrintWriter out = response.getWriter();
		out.print(responseText);
		out.close();*/
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	@Override
	public void init() throws ServletException {
		// Put your code here
	}

}
